<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片部分 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="openAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <treeTable
        :data="cateList"
        :columns="columns"
        :show-row-hove="false"
        :show-row-hover="false"
        :border="true"
        :show-index="true"
        :selection-type="false"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted == false"
            style="color: green"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 等级 -->
        <template slot="level" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </treeTable>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加商品分类"
      :visible.sync="cateDialogVisible"
      width="30%"
      @close="cateDialogClosed"
    >
      <el-form
        :model="cateFormData"
        :rules="cateFormDataRules"
        label-width="100px"
        class="demo-ruleForm"
        ref="cateFormDataRef"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateFormData.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
            <!-- 级联下拉项 -->
          <el-cascader
            v-model="selectedParentCate"
            :options="parentCateList"
            :props="cascaderProps"
            @change = "cascaderChange"
            :clearable="true"
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate" 
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品分类查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //所有商品类别
      total: 0,
      //本页商品分类列表
      cateList: [],
      //树表格列配置
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "400px",
        },
        {
          label: "是否有效",
          //表示，将当前列定义为模板列
          type: "template",
          //表示当前这一列使用模板名称
          template: "isok",
        },
        {
          label: "等级",
          //表示，将当前列定义为模板列
          type: "template",
          //表示当前这一列使用模板名称
          template: "level",
        },
        {
          label: "操作",
          //表示，将当前列定义为模板列
          type: "template",
          //表示当前这一列使用模板名称
          template: "operation",
        },
      ],
      //添加商品分类对话框标志位
      cateDialogVisible: false,
      //添加商品分类表单对象
      cateFormData: {
        //分类父ID
        cat_pid: 0,
        //分类名称
        cat_name: "",
        //分类层级
        cat_level: 0,
      },
      //商品分类表单验证规则
      cateFormDataRules: {
        cat_name: [
          { required: true, message: "请输入商品分类名称", trigger: "blur" },
        ],
      },
      //获取到的父级分类
      parentCateList: [],
      //选择到的父级分类数组
      selectedParentCate: [],
      //级联显示设置
      cascaderProps: {
          expandTrigger: 'hover',
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
      }
    };
  },
  created() {
    //获取商品分类列表数据
    this.queryGoodCateLit();
  },
  methods: {
    // 获取商品分类列表数据
    async queryGoodCateLit() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });

      if (res.meta.status != 200) {
        return this.$message.error("获取商品分类列表失败");
      }

      this.total = res.data.total;
      this.cateList = res.data.result;
    },
    //监听页码变化
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      //重新发起请求，获取列表数据
      this.queryGoodCateLit();
    },
    //监听页面容量变化
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      //重新发起请求，获取列表数据
      this.queryGoodCateLit();
    },
    //添加商品分类对话框
    async openAddCateDialog() {
      this.cateDialogVisible = true;
      //发送请求，获取所有一级、二级商品分类
      const { data: res } = await this.$http.get("/categories", {
        params: {
          type: 2,
        },
      });
      if (res.meta.status != 200)
        return this.$message.error("商品分类获取失败");
      //将获取到的一级、二级商品分类存入列表中
      this.parentCateList = res.data;
    },
    //商品分类级选项变动时
    cascaderChange(){
        if(this.selectedParentCate.length > 0){
            //设置父级分类的ID
            this.cateFormData.cat_pid = this.selectedParentCate[this.selectedParentCate.length -1]
            //为房钱分类的等级赋值
            this.cateFormData.cat_level = this.selectedParentCate.length
        }else{
            //设置父级分类的ID
            this.cateFormData.cat_pid = 0
            //为房钱分类的等级赋值
            this.cateFormData.cat_level = 0
        }
        console.log(this.cateFormData);
    },
    //监听对话框关闭，重置表单数据
    cateDialogClosed(){
        this.$refs.cateFormDataRef.resetFields()
        this.selectedParentCate = [],
        this.cateFormData.cat_pid = 0;
        this.cateFormData.cat_cat_level = 0;
    },
    //监听分类提交按钮
    async addCate(){
        const validateRes = await this.$refs.cateFormDataRef.validate().catch(err => err)
        if(!validateRes) return this.$message.error('数据验证不通过')
        const { data: res } = await this.$http.post('/categories',this.cateFormData)
        if(res.meta.status != 201) return this.$message.error('分类添加失败')
        this.$message.success('分类添加成功')
        this.cateDialogVisible =false;
        this.queryGoodCateLit()
    }
  },
};
</script>

<style lang="less" scoped>
.el-cascader{
    width: 100%;
}
</style>